package com.tbit.main.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.tbit.common.constant.OperationPlatform;
import com.tbit.common.entity.MachineOpLog;
import com.tbit.main.constant.DaoParamConstant;
import com.tbit.main.controller.interceptor.LoginUserContextHolder;
import com.tbit.main.dao.log.PatrolLogDao;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.PatrolLog;
import com.tbit.main.pojo.PatrolLogSta;
import com.tbit.main.service.AccountUserService;
import com.tbit.main.service.GeoreGionService;
import com.tbit.main.service.PatrolLogService;
import com.tbit.main.service.RoleService;
import com.tbit.main.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LMY
 * @create 2021-01-08 16:15
 * 巡检
 */
@Service("patrolLogService")
public class PatrolLogServiceImpl implements PatrolLogService {

    @Autowired
    private PatrolLogDao patrolLogDao;
    @Autowired
    private AccountUserService accountUserService;
    @Autowired
    private GeoreGionService georeGionService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private MachineOpLogService machineOpLogService;

    @Override
    public Integer insert(PatrolLog patrolLog) {
        Integer result = patrolLogDao.insert(patrolLog);

        AccountUser accountUser = LoginUserContextHolder.getLoginUser();

        // 插入车辆操作日志
        machineOpLogService.insertSingle(MachineOpLog.builder()
                .accountId(patrolLog.getAccountId())
                .machineId(patrolLog.getMachineId())
                .machineNO(patrolLog.getMachineNO())
                .userCode(patrolLog.getUserCode())
                .operation("巡检")
                .opUser(accountUser.getName())
                .opUserPhone(accountUser.getPhone())
                .opPlatform(OperationPlatform.MAINTAIN.code)
                .result(true)
                .opTime(LocalDateTime.now())
                .build()
        );

        return result;
    }

    @Override
    public List<PatrolLogSta> groupByTime(Integer agentId, Integer userType, Integer usermainId,
                                          String startTime, String endTime, Integer isValid, List<Integer> regionId) {
        List<PatrolLogSta> dlses = new ArrayList<>();

        /**查询代理商运维账号*/
        List<Integer> accountUserIds = new ArrayList<Integer>();
        /**判断当前账号是不是运维人员*/
        if (roleService.getRoleCode(userType).equals(1)) {
            accountUserIds.add(usermainId);
        }

        Map<String, Object> params = new HashMap<String, Object>();
        params.put(DaoParamConstant.accountId, agentId);
        params.put("userMainIds", StringUtil.getListString(accountUserIds));
        params.put("isValid", isValid);
        params.put(DaoParamConstant.startTime, startTime);
        params.put(DaoParamConstant.endTime, endTime);
        if (CollUtil.isEmpty(regionId)) {
            params.put("exist", "'exist'");
            regionId = georeGionService.getByAccountUserId(usermainId);
        } else {
            params.put("exist", null);
        }
        params.put("regionId", StringUtil.getListString(regionId));
        dlses = patrolLogDao.groupByTime(params);

        List<PatrolLogSta> dlsefilters = new ArrayList<>();
        if (dlses.size() > 0) {
            for (PatrolLogSta patrolLogSta : dlses) {
                AccountUser au = accountUserService.getById(patrolLogSta.getAccountUserId());
                if (au != null) {
                    patrolLogSta.setName(au.getName());
                    patrolLogSta.setPhone(au.getPhone());
                    dlsefilters.add(patrolLogSta);
                }
            }
        }

        return dlsefilters;
    }

    @Override
    public List<PatrolLog> getByTime(Integer accountId, Integer userMainId, String startTime, String endTime, Integer isValid, List<Integer> regionId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(DaoParamConstant.accountId, accountId);
        params.put("userMainId", userMainId);
        params.put("isValid", isValid);
        params.put(DaoParamConstant.startTime, startTime);
        params.put(DaoParamConstant.endTime, endTime);
        if (CollUtil.isEmpty(regionId)) {
            params.put("exist", "'exist'");
            regionId = georeGionService.getByAccountUserId(userMainId);
        } else {
            params.put("exist", null);
        }
        params.put("regionId", StringUtil.getListString(regionId));
        return patrolLogDao.getByTime(params);
    }
}
